<?php
/**
 * 	Bushido
 *
 * @category   Bushido
 * @package    Bushido
 * @copyright  Copyright (c) 2008-2009 Nathan Keyes
 * @author $Author: nkeyes $
 * @version    $Id: User.php 17 2009-11-15 01:58:48Z nkeyes $
 */
 
class Bushido_DbTable_User extends Bushido_Db_Table_Abstract
{
	protected $_name = 'bushido_user';
	
	protected $_sequence = true;
	
	protected $rowClass = 'Bushido_DbTable_User_Row';
	
	protected $_referenceMap = array
	(
	);
	
	protected $_childMap = array
	(
		'Roles' => array
		(
			'refTableClass' => 'Bushido_DbTable_UserRole',
			'cardinality' => Bushido_Db_Table_Abstract::ONE_OR_MANY
		),
	);
	
	
	/**
	 * 
	 * @param $config
	 * @return void
	 */
	/*
	public function __construct($config = array())
	{
		if (!is_array($config))
		{
			$config = array(self::ADAPTER => $config);
		}
        $config[self::ROW_CLASS] = 'Bushido_DbTable_User_Row';
		parent::__construct($config);
	}
	*/
	
	/**
	 * (non-PHPdoc)
	 * @see library/Bushido/Db/Table/Bushido_Db_Table_Abstract#getSearchableFields()
	 */
	public function getSearchableFields()
	{
	}
	
	
	/**
	 * (non-PHPdoc)
	 * @see library/Bushido/Db/Table/Bushido_Db_Table_Abstract#_setFieldDetails()
	 */
	protected function _setFieldDetails()
	{
		//bushido_user_id
		$this->_metadata['bushido_user_id']['fieldDetails'] = array
		(
			'options' => array
			(
				'label' => 'Employee Number:',
				'required' => true,
				'invalidMessage' => 'Please enter the user\'s Employee Number.'
			) 
		);
		
		//first_name
		$this->_metadata['first_name']['fieldDetails'] = array
		(
			'options' => array
			(
				'label' => 'First Name:',
				'required' => true,
				'invalidMessage' => 'Please enter the user\'s First Name.'
			) 
		);
		
		//last_name
		$this->_metadata['last_name']['fieldDetails'] = array
		(
			'options' => array
			(
				'label' => 'Last Name:',
				'required' => true,
				'invalidMessage' => 'Please enter the user\'s Last Name.'
			) 
		);
		
		//login
		$this->_metadata['login']['fieldDetails'] = array
		(
			'options' => array
			(
				'label' => 'Network Login:',
				'required' => true,
				'validators' =>
				array
				(
									
				),
				'invalidMessage' => 'Please enter the user\'s Network Login.'
			) 
		);
		
		//email
		$this->_metadata['email']['fieldDetails'] = array
		(
			'options' => array
			(
				'label' => 'Email Address:',
				'required' => true,
				'validators' =>
				array
				(
					array('EmailAddress', false),
									
				),
				'invalidMessage' => 'Please enter the user\'s Email Address.'
			) 
		);
		
		
		//is_enabled
		$this->_metadata['is_enabled']['fieldDetails'] = array
		(
			'type' => 'CheckBox',
			'options' => array
			(
				'label' => 'Enabled:',
				'required' => true,
				'checkedValue' => 1,
				'uncheckedValue' => 0,
				'checked' => true
			
			) 
		);
	}
	
	/**
	 * (non-PHPdoc)
	 * @see library/Bushido/Db/Table/Bushido_Db_Table_Abstract#getDisplayValue()
	 */
	public static function getDisplayValue(Zend_Db_Table_Row_Abstract $row, $context = null)
	{
		return $row->first_name . ' ' . $row->last_name;
	}
	
	
	/**
	 * 
	 * @param $user
	 * @return array
	 */
	public static function getRoles($bushido_user_id)
	{
		if(($bushido_user_id instanceof Bushido_DbTable_User))
		{
			$bushido_user_id = $bushido_user_id->bushido_user_id;
		}
		$roles = Bushido_Db_Table::getAdapter()->fetchCol("SELECT bushido_role_id FROM bushido_user_2_bushido_role WHERE bushido_user_id = ?", $bushido_user_id);
		return $roles;
	}
	
	/**
	 * 
	 * @param $user
	 * @param $role
	 * @return boolean
	 */
	public static function hasRole($user, $roles)
	{
		if(!is_array($roles))
		{
			$roles = array($roles);
		}
		elseif(!in_array(Bushido_DbTable_Role::ADMIN, $roles))
		{
			array_push($roles, Bushido_DbTable_Role::ADMIN);
		}
		$userRoles = self::getRoles($user);
		
		foreach($roles as $role)
		{
			if(in_array($role, $userRoles))
			{
				return true;
			}
		}
		return false;
		
	}
	
	protected static function _getUser($user)
	{
		if(!($user instanceof Zend_Db_Table_Abstract))
		{
			$user = Bushido_Db_Table::getTable('Bushido_DbTable_User')->find($user)->current();
		}
		elseif(!($user->getTable() instanceof Bushido_DbTable_User))
		{
			throw new Exception ("parameter 1 to _getUser must be a row from Bushido_DbTable_User or an id of a user");
		}
		return $user;
	}
}